﻿/// <reference path="duomi.common.js" />
/*************************************************************
Base Common bootstrap V3 model Js.
模式窗口类
Create By Ming.ma
2014-09-02 V1.0
*************************************************************/
Namespace.register("$ext.model");

(function (window) {
    "use strict";
    $ext.model = {
        $description: '模式窗口类',
        $author: 'Ming.ma',
        $version: '1.0.0',
        //提示框
        alert: function (context) {
            this.dialog({
                title: '提示',
                context: context,
                closeButton: false,
                modelClass: 'modal-sm'
            });
        },
        //确认框
        confirm: function (context, callback) {
            this.dialog({
                title: '确认提示',
                context: context,
                closeButton: true,
                modelClass: 'modal-sm',
                callback: callback
            });
        },
        //弹出框
        dialog: function (option) {
            var flag = false;
            $(createModelDiv(option)).modal().on('hidden.bs.modal', function (e) {
                flag || option.callback(false);
                //弹出框关闭时，将model对象删除
                $$$("mymodel_ruyo_net").parentElement.removeChild($$$("mymodel_ruyo_net"));
            });
            $("#mymodel_ruyo_net button.btn-primary").on("click", function () {
                flag = true;
                option.callback(true);
            });
        }
    };

    function createModelDiv(option) {
        var defaultOption = {
            title: '提示信息', //标题
            context: '提示的内容，支持HTML',//内容
            closeButton: true,//是否显示关闭按钮
            cancelButton: true,//是否显示取消按钮
            saveButton: true,//是否显示确认按钮
            modelClass: '',//弹出框大小（modal-lg/modal-sm）
            callback: function () { }//弹出框被关闭回调（确认：true, 取消：false）
        }

        option = leftCopyJson(defaultOption, option);

        if (option) {
            var model = $dom.CE("div", { "class": "modal fade", "role": "dialog", "aria-hidden": "true", "tabindex": "-1", "id": "mymodel_ruyo_net" });
            var mdialog = $dom.CE("div", { "class": "modal-dialog " + option.modelClass || '' });
            var mcontent = $dom.CE("div", { "class": "modal-content" });
            var mheader = $dom.CE("div", { "class": "modal-header" });
            mheader.innerHTML += option.closeButton ? '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>' : '';
            mheader.innerHTML += $string.format('<h4 class="modal-title">{0}</h4>', option.title);
            var mbody = $dom.CE("div", { "class": "modal-body" });
            mbody.innerHTML = option.context;//提示内容
            var mfooter = $dom.CE("div", { "class": "modal-footer" });
            mfooter.innerHTML += option.saveButton ? '<button type="button" class="btn btn-primary" name="saveBtn" data-dismiss="modal">确定</button>' : '';
            mfooter.innerHTML += option.closeButton ? '<button type="button" class="btn btn-default" data-dismiss="modal" name="closeBtn">关闭</button>' : '';

            mcontent.appendChild(mheader);
            mcontent.appendChild(mbody);
            mcontent.appendChild(mfooter);
            mdialog.appendChild(mcontent);
            model.appendChild(mdialog);
            document.body.appendChild(model);
            return model;
        }
    }



    function leftCopyJson(jsn1, jsn2) {
        var jsn = {};
        for (var j in jsn1)
            jsn[j] = $object.isNull(jsn2 && jsn2[j]) ? jsn1[j] : jsn2[j];
        return jsn;
    }
})(window);